package cn.sjx.org.controller;

import cn.sjx.auth.annotation.Permission;
import cn.sjx.basic.utils.AjaxResult;
import cn.sjx.basic.utils.PageList;
import cn.sjx.org.domain.Department;
import cn.sjx.org.query.DepartmentQuery;
import cn.sjx.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Permission(name = "部门管理" ,descs = "部门管理权限")
@RestController
@RequestMapping("/department")
//接口文档注释
@Api(value = "部门的API",description = "部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;


    @Permission(name = "查询所有" ,descs = "查询所有部门")
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public AjaxResult getAll() {

        try {
            List<Department> list = departmentService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }

    @Permission(name = "查询部门树" ,descs = "查询部门树")
    @GetMapping("/tree")
    public AjaxResult tree(){
        try {
            List<Department> list = departmentService.tree();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }


    @Permission(name = "通过ID查询" ,descs = "通过ID查询部门")
    //查询单个
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Serializable id) {
        try {
            Department department = departmentService.getById(id);
            return AjaxResult.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }


    @Permission(name = "新增/修改" ,descs = "新增/修改部门")
    //增加或修改可以共用一个接口，根据是否有id传入来分辨
    @PutMapping
    @ApiOperation(value = "新增或更新" )
    public AjaxResult insertOrupdate(@RequestBody Department department) {
        //通过Objects.isNull来判断
        try {
            if (Objects.isNull(department.getId())) {
                departmentService.insert(department);
                return AjaxResult.me();
            } else {
                departmentService.update(department);
                return AjaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    @Permission(name = "删除" ,descs = "删除部门")
    //按ID删除
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Serializable id){
        try {
            departmentService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }


    @Permission(name = "分页查询" ,descs = "分页查询部门")
    @PostMapping
    public AjaxResult pageList(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> pageList = departmentService.pageList(departmentQuery);
        return AjaxResult.me().setData(pageList);

    }


    @Permission(name = "批量删除" ,descs = "批量删除部门")
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
